package hn.sigit.logic.bpm;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;


@Name("documentViewer")
@Scope(value = ScopeType.SESSION)
public class DocumentViewer {
	private static HashMap<String, String> mimeMap = new HashMap<String, String>();
	private static final String downloadMIME = "application/x-download";
	
	private String documentPath;
	private File file2View;
	private boolean forceDownload = false;

	static {
		mimeMap.put(".jpeg", "image/jpeg");
		mimeMap.put(".jpg", "image/jpeg");
		mimeMap.put(".pdf", "application/pdf");
		mimeMap.put(".tif", "image/x-tiff");
		mimeMap.put(".tiff", "image/x-tiff");
		mimeMap.put(".png", "image/x-png");
	}
	
	public String getDocumentPath() {
		return documentPath;
	}
	public void setDocumentPath(String documentPath) {
		this.documentPath = documentPath;
		
		file2View = new File(documentPath);
	}
	
	public boolean isForceDownload() {
		return forceDownload;
	}
	public void setForceDownload(boolean forceDownload) {
		this.forceDownload = forceDownload;
	}
	
	public String getAppropriateMIME() {
		if (forceDownload)
			return downloadMIME;
		
		String fileExt = documentPath.substring(documentPath.lastIndexOf('.'));
		
		if (!mimeMap.containsKey(fileExt))
			return downloadMIME;
		
		return mimeMap.get(fileExt);
	}
	
	public void downloadFile() {
		/*
        FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		HttpServletResponse response = (HttpServletResponse) externalContext
				.getResponse();

		response.reset();
		
		response.setContentType(getAppropriateMIME());
		
		response.setHeader("Content-disposition",
				"attachment; filename=\"shape.zip\"");

		BufferedOutputStream output = new BufferedOutputStream(out);

		try {
			writeZipFile(shapeFiles, output);
		} finally {
			output.close();
		}
		*/
	}
	
	public void viewFile(OutputStream out, Object data) {
		if (!file2View.exists()) return;
		
		BufferedOutputStream output = new BufferedOutputStream(out);
		final int BUFFER = 4192;

		byte[] buff = new byte[BUFFER];
		BufferedInputStream origin = null;

		try {
			FileInputStream fis = new FileInputStream(file2View);
			origin = new BufferedInputStream(fis, BUFFER);
			
			int count;
			while ((count = origin.read(buff, 0, BUFFER)) != -1)
				output.write(buff, 0, count);
		}
		catch (FileNotFoundException fnfe) {
			return;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				if (null != origin) origin.close();
				output.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
